/**
 * 
 */
package com.yft.data.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.yft.data.dao.PublicDao;
import com.yft.data.util.StringUtil;

/**
 * @author Ruibron
 *
 */
@Service
public class UserService {
	private Logger logger = Logger.getLogger(UserService.class);
	@Autowired
	private PublicDao publicDao ;

	/**
	 * 获取用户的info，用来查看是否过期
	 * @param corpid
	 * @param userid
	 * @return
	 */
	@Cacheable(value="userexpire", key="#userid+#corpid")
	public String getUserPayTime(int corpid, String userid) {
		StringBuffer sb = new StringBuffer();
		sb.append("select nextpaytime from user where ");
		sb.append("corpid = "+corpid);
		sb.append(" and userid = '"+userid+"'");
		String sql = sb.toString();
		logger.debug(sql);
		Map<String,Object> map = publicDao.getObjectMap(sql);
		String nextpayStr = (String) map.get("nextpaytime");
		return nextpayStr;
	}
	/**
	 *  查找该公司的该用户名是否存在
	 * @param corpid
	 * @param userid
	 * @return
	 */
	@Cacheable(value="finduser", key="#userid+#corpid")
	public boolean find(int corpid, String userid) {
		StringBuffer sb = new StringBuffer();
		sb.append("select count(*) from user where ");
		sb.append("corpid = "+corpid);
		sb.append(" and userid = '"+userid+"'");
		String sql = sb.toString();
		if(0==publicDao.getCount(sql))
		{
			return false;
		}else
		{
			return true;
		}
		
	}
	/**
	 * @param corpid
	 * @param userid
	 * @param username
	 * @param cityid
	 */
	@CacheEvict(value="finduser", key="#userid+#corpid")
	public boolean add(int corpid, String userid, String username, int cityid,int testDays) {
		// 添加用户
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		Date date2 = StringUtil.getDateAfter(date,testDays);
        //试用期，默认到期时间为一个月之后
        String nextpaytime = simpleDateFormat.format(date2);
        String lastpaytime = simpleDateFormat.format(date);
		StringBuffer sb = new StringBuffer();
		sb.append("insert into user (userid,corpid,username,cityid,lastpaytime,nextpaytime,paytotal) values ( ");
		sb.append("'"+userid+"',");
		sb.append("'"+corpid+"',");
		sb.append("'"+username+"',");
		sb.append("'"+cityid+"',");
		sb.append("'"+lastpaytime+"',");
		sb.append("'"+nextpaytime+"',");
		sb.append("'0')");
		String sql = sb.toString();
		logger.debug(sql);
		if(1==publicDao.update(sql))
		{
			return true;
		}
		return false;
	}
	/**
	 * @param corpid
	 * @param userid
	 * @param paynum
	 * @param nextpaytime
	 * @return
	 */
	@CacheEvict(value="userexpire", key="#userid+#corpid")
	public boolean pay(int corpid, String userid, float paynum,
			String nextpaytime) {
		// 用户付款
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		Date now = new Date();
		String lastpaytime = dateFormat.format(now);
		List<String> list = new ArrayList<String>(); 
		StringBuffer sb = new StringBuffer();
		sb.append("update user set ");
		sb.append("paytotal = paytotal + "+paynum);
		sb.append(", nextpaytime = "+nextpaytime);
		sb.append(", lastpaytime = "+lastpaytime);
		sb.append(" where ");
		sb.append("corpid = "+corpid);
		sb.append(" and userid = '"+userid+"'");
		String sql = sb.toString();
		list.add(sql);
		logger.debug(sql);
		sb = new StringBuffer();
		sb.append("insert into payment (userid,corpid,paymoney,paytime) values (");
		sb.append("'"+userid+"',");
		sb.append("'"+corpid+"',");
		sb.append("'"+paynum+"',");
		sb.append("'"+lastpaytime+"')");
		String sql2 = sb.toString();
		list.add(sql2);
		String[] strs = new String[2];
		int[] results = publicDao.batchUpdate(list.toArray(strs));
		if(results[0]==1 && results[1]==1)
		{
			return true;
		}
		return false;
	}
	public Map<String,Object> getUserByNameAndPwd(String username, String password) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT USERID,USERNAME,PASSWORD,STATUS,DESCN,USERID,ID FROM USERS WHERE ");
		sb.append("USERNAME = '"+username);
		sb.append("' AND PASSWORD = '"+password+"'");
		String sql = sb.toString();
		logger.debug(sql);
		Map<String,Object> map = publicDao.getObjectMap(sql);
		return map;
	}
	
	public List<Map<String, Object>> getResourceByRole(Integer id) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT RESC_ID,RESCNAME,RESC_TYPE,RESC_STRING,PRIORITY,DESC FROM RESC WHERE ");
		sb.append("RESC_ID IN (");
		sb.append("SELECT RESC_ID FROM ROLERESC WHERE ROLEID IN (");
		sb.append("SELECT ROLEID FROM USERROLE WHERE USERID = "+id);
		sb.append(")");
		sb.append(")");
		String sql = sb.toString();
		logger.debug(sql);
		List<Map<String, Object>> list = publicDao.getObjectList(sql);
		return null;
	}
}
